import axios from 'axios';
import {ElMessage} from "element-plus";
import UserDto from '../class/UserDto'
export const getFoodList= ()=>{
    return axios.get("http://localhost:8080/food/foodList")
}
export const searchFood= (name)=>{
    return axios.get("http://localhost:8080/food/search?name="+name.a)
}
export const getusercalorie2= (uid)=>{
    return axios.get("http://10.6.248.202:8080/usercalorie/usersevenday?uid="+uid.a)
}
export const getcalorieList=(uid)=>{
    return axios.get("http://10.6.248.202:8080/usercalorie/calorieList?uid="+uid.a)
}
export const getpersonList= ()=>{
    return axios.get("http://localhost:8080/user/userList")
}
export const getsportList= ()=>{
    return axios.get("http://localhost:8080/sport/sportList")
}
export const deleteSport= (name)=>{
    return axios.post("http://localhost:8080/sport/delete",name)
}
export const searchUser= (height)=>{
    return axios.get("http://localhost:8080/user/byh?height="+height.a)
}
export const login=(req)=>{
    return axios.get('http://localhost:8080/user/login?username='+req.a+'&password='+req.b)
}
const defaultError = () =>ElMessage.error('发生了一些错误，请联系管理员')
const defaultFailure = (message) =>ElMessage.warning(message)
    function post(url,data ,success,failure = defaultFailure,error = defaultError ) {

        axios.post(url,data,{
            headers:{
                'Content-Type' : 'application/x-www-form-urlencoded'
            },
            withCredentials:true
        }).then(({data}) =>{
            if(data.success)
                success(data.message,data.status)

        })
    }
    function get(url,success,failure = defaultFailure,error = defaultError){
    axios.post(url,{
        withCredentials:true
    }).then(({data}) =>{
        if(data.success)
            success(data.message,data.status)

    })
}
export {get,post}
